package com.sogou.adm.bizdev.dataimport.web;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.sogou.adm.bizdev.dataimport.service.TriggerManager;
import com.sogou.adm.bizdev.dataimport.vo.DataJobTrigger;

@Results( { @Result(name = CrudActionSupport.RELOAD, location = "trigger.action", type = "redirect") })
public class TriggerAction extends CrudActionSupport<DataJobTrigger> {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6340364582945850192L;

	private DataJobTrigger entity;

	private String name;
	private String group;
	private String jobName;
	private String jobGroup;
	@Autowired
	private TriggerManager triggerManager;


	private Page<DataJobTrigger> page = new Page<DataJobTrigger>(5);

	public Page<DataJobTrigger> getPage() {
		return page;
	}

	@Override
	public String execute() throws Exception {
		return list();
	}

	@Override
	public String list() throws SchedulerException {
		List<DataJobTrigger> result = triggerManager.getAll();
		page.setTotalCount(result.size());
		page.setResult(result);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {

		triggerManager.addTrigger(entity);
		addActionMessage("保存触发器成功");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		triggerManager.deleteTrigger(name, group);
		addActionMessage("删除触发器成功");
		return RELOAD;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	public String getJobName() {
		return jobName;
	}

	public void setJobName(String jobName) {
		this.jobName = jobName;
	}

	public String getJobGroup() {
		return jobGroup;
	}

	public void setJobGroup(String jobGroup) {
		this.jobGroup = jobGroup;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (name != null) {
			entity = triggerManager.get(name, group);
		} else {
			entity = new DataJobTrigger();
		}

	}

	public DataJobTrigger getModel() {
		return entity;
	}
}
